package com.tycmc.signelectroniccontracts.tools.update;

import android.app.Dialog;
import android.content.Context;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.tycmc.signelectroniccontracts.R;
import com.tycmc.signelectroniccontracts.tools.images.FileSizeUtil;


/**
 * Created by GY on 2018/1/21.
 */

public class DownLoadProgerss {
    private Dialog dialog;
    private ProgressBar pro;
    private TextView tvSpeed;
    private TextView tvSize;
    private View.OnClickListener onClick;

    public DownLoadProgerss(Context context) {
        WindowManager windowManager = (WindowManager) context.getSystemService(context.WINDOW_SERVICE);
        Display display = windowManager.getDefaultDisplay();
        View view = ((LayoutInflater) context.getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE))
                .inflate(R.layout.layout_download, null);
        View llMain = view.findViewById(R.id.ll_main);
        initView(view);
        //自定义Dialog布局和参数
        dialog = new Dialog(context, R.style.AlertDialogStyle);
        dialog.setContentView(view);
        int width = display.getWidth();
        int height = display.getHeight();
        //调整dialog背景大小
        llMain.setLayoutParams(new FrameLayout.LayoutParams((int) ((width < height ? width : height) * 0.85), LinearLayout.LayoutParams.WRAP_CONTENT));
        dialog.setCanceledOnTouchOutside(false);
        dialog.setCancelable(false);
    }

    private void initView(View view) {
        pro = ((ProgressBar) view.findViewById(R.id.progress_dialog));
        pro.setMax(100);
        tvSpeed = ((TextView) view.findViewById(R.id.tv_speed));
        tvSize = ((TextView) view.findViewById(R.id.tv_size));
//        后台下载
        view.findViewById(R.id.bt_backdown).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dimiss();
                if (onClick != null) {
                    onClick.onClick(v);
                }
            }
        });
//        取消
        view.findViewById(R.id.bt_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dimiss();
                if (onClick != null) {
                    onClick.onClick(v);
                }
            }
        });
    }
    public void setOnClick(View.OnClickListener onClick){
        this.onClick = onClick;
    }

    public void setPro(int maxSize, int size) {
        int i = (int) ((size * 100.0) / maxSize);
        pro.setProgress(i);
        tvSize.setText(FileSizeUtil.FormetFileSize(size) + "/" + FileSizeUtil.FormetFileSize(maxSize));
    }
    public void show() {
        if (dialog != null) {
            dialog.show();
        }
    }

    public void dimiss() {
        if (dialog != null&&dialog.isShowing()) {
            dialog.dismiss();
        }
    }
}

